package com.example.restatr.Atcivity.LogisticsQuery;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.LogisticsQuery.Bean.LogisticsCompanyInfoBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class LogisticsCompanyAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private ImageView head;
    private TextView name;
    private TextView phone;
    private TextView workTime;
    private TextView introduce;
    private LinearLayout priceList;
    private RecyclerView priceGrid;
    private LinearLayout newsList;
    private RecyclerView rv;
    private LogisticsCompanyInfoBean.DataBean dataBeans;
    private List<LogisticsCompanyInfoBean.DataBean.PriceListBean> priceListBeans = new ArrayList<>();
    private List<LogisticsCompanyInfoBean.DataBean.NewsListBean> newsListBeans = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logistics_company_at);

        toolbar = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        head = findViewById(R.id.head);
        name = findViewById(R.id.name);
        phone = findViewById(R.id.phone);
        workTime = findViewById(R.id.workTime);
        introduce = findViewById(R.id.introduce);
        priceList = findViewById(R.id.priceList);
        priceGrid = findViewById(R.id.priceGrid);
        newsList = findViewById(R.id.newsList);
        rv = findViewById(R.id.rv);
        Tools.SetBarTitle(toolbar, barTitle, getIntent().getStringExtra("title")
                , search, this, false);
        priceListBeans.add(new LogisticsCompanyInfoBean.DataBean.PriceListBean("目的地", "文件首重/g", "文件递增价格/元", "物品首重/kg", "物品递增价格/百"));
        GetDetailsTheLogisticsCompany();
    }

    private void GetDetailsTheLogisticsCompany() {
        new Thread(() -> {
            String get = Tools.Get("/prod-api/api/logistics-inquiry/logistics_company/" + getIntent().getStringExtra("id"),
                    MainActivity.sp.getString("token", ""));
            LogisticsCompanyInfoBean logisticsCompanyInfoBean = new Gson().fromJson(get, LogisticsCompanyInfoBean.class);
            dataBeans = logisticsCompanyInfoBean.getData();
            runOnUiThread(() -> {
                if (dataBeans != null) {
                    SetInfo();
                }
            });
        }).start();
    }

    private void SetInfo() {
        Glide.with(this)
                .load(Tools.ip + dataBeans.getImgUrl())
                .transform(new RoundedCorners(15))
                .into(head);
        name.setText(dataBeans.getName());
        phone.setText(dataBeans.getPhone());
        workTime.setText(dataBeans.getShippingMethod());
        introduce.setText(dataBeans.getIntroduce());
        if (dataBeans.getPriceList().size() > 0) {
            priceListBeans.addAll(dataBeans.getPriceList());
            priceList.setVisibility(View.VISIBLE);
            priceGrid.setAdapter(new adapter());
        }
        if (dataBeans.getNewsList().size() > 0) {
            newsListBeans.addAll(dataBeans.getNewsList());
            newsList.setVisibility(View.VISIBLE);
            rv.setAdapter(new NewsAdapter());
        }
    }

    public void onClick(View view) {
        Intent intent = new Intent(LogisticsCompanyAt.this, CreateComplaintAt.class);
        intent.putExtra("id", dataBeans.getId());
        startActivity(intent);
    }

    class adapter extends RecyclerView.Adapter<adapter.viewHolder> {

        @NonNull
        @Override
        public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate(R.layout.price_item_layout, parent, false);
            return new viewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull viewHolder holder, int position) {
            if (position == 0) {
                holder.llItem.setBackgroundColor(Color.parseColor("#95c5ef"));
            } else if (position % 2 != 0) {
                holder.llItem.setBackgroundColor(Color.parseColor("#EAEAE9"));
            } else {
                holder.llItem.setBackgroundColor(Color.parseColor("#F5F5F5"));
            }
            holder.areaName.setText(priceListBeans.get(position).getAreaName());
            holder.fileStart.setText(String.valueOf(priceListBeans.get(position).getFileStart()));
            holder.fileStep.setText(String.valueOf(priceListBeans.get(position).getFileStep()));
            holder.objStart.setText(String.valueOf(priceListBeans.get(position).getObjStart()));
            holder.objStep.setText(String.valueOf(priceListBeans.get(position).getObjStep()));
        }

        @Override
        public int getItemCount() {
            return priceListBeans.size();
        }

        class viewHolder extends RecyclerView.ViewHolder {
            LinearLayout llItem;
            TextView areaName;
            TextView fileStart;
            TextView fileStep;
            TextView objStart;
            TextView objStep;

            public viewHolder(@NonNull View itemView) {
                super(itemView);
                llItem = itemView.findViewById(R.id.ll_item);
                areaName = itemView.findViewById(R.id.areaName);
                fileStart = itemView.findViewById(R.id.fileStart);
                fileStep = itemView.findViewById(R.id.fileStep);
                objStart = itemView.findViewById(R.id.objStart);
                objStep = itemView.findViewById(R.id.objStep);
            }
        }
    }

    class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.viewHolder> {

        @NonNull
        @Override
        public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate(R.layout.xw_card_item, parent, false);
            return new viewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull viewHolder holder, int position) {
            holder.bottomLl.setVisibility(View.GONE);
            holder.title.setText(newsListBeans.get(position).getTitle());
            Glide.with(LogisticsCompanyAt.this)
                    .load(Tools.ip + newsListBeans.get(position).getImgUrl())
                    .transform(new RoundedCorners(15))
                    .into(holder.tp);
            holder.rl.setCardBackgroundColor(Color.parseColor("#F5F5F5"));
        }

        @Override
        public int getItemCount() {
            return newsListBeans.size();
        }

        class viewHolder extends RecyclerView.ViewHolder {
            private CardView rl;
            private TextView title;
            private ImageView tp;
            private LinearLayout bottomLl;

            public viewHolder(@NonNull View itemView) {
                super(itemView);
                rl = itemView.findViewById(R.id.Rl);
                title = itemView.findViewById(R.id.title);
                tp = itemView.findViewById(R.id.tp);
                bottomLl = itemView.findViewById(R.id.bottom_ll);
            }
        }
    }


}